﻿(function (ns) {
    ZYCLib.RegNameSpace(ns).Sign = (function () {
        var isPass = true;
        var procceing = false;
        var ShowErrorMessage = function (message, hideTime, fnCallBack) {
            ZYCLib.UI.MessageBox.Alert(message, hideTime, fnCallBack);
            isPass = false;
        };

        var HideErrorMessage = function () {
            isPass = true;
        };

        var CheckInput = function (id, message, regExp, ismacth) {
            var isValidated = ZYCLib.Text.InputValidator(id, regExp, ismacth);
            if (!isValidated) {
                ShowErrorMessage(message, 0, function () {
                    $("#" + id).focus();
                });//不写回调函数，当点击确定按钮关闭弹出对话框后获取到焦点的控件将失去焦点。写该回调函数仅为解决弹出对话框关闭后设置控件焦点。
            }
            return isValidated;
        };

        var ResetPassword = function () {
            HideErrorMessage();
            if (!CheckInput("txtPassword", "请输入密码")) return;
            if (!CheckInput("txtPassword", "密码格式不正确", "^[A-Za-z0-9]{6,16}$", true)) return;
            if (!CheckInput("txtConfirmPassword", "请再次输入密码")) return;
            var userName = $("#txtUserName").val();
            var smsMobile = $("#txtSMSMobile").val();

            var newPwd = $("#txtPassword").val();
            if (newPwd === userName) {
                ShowErrorMessage("密码不能和用户名相同", 0);
                self.isClickChangePwdPostButton = false;
                return;
            }
            if (newPwd === smsMobile) {
                ShowErrorMessage("密码不能和手机号相同", 0);
                self.isClickChangePwdPostButton = false;
                return;
            }

            if (newPwd !== $("#txtConfirmPassword").val()) {
                ShowErrorMessage("两次输入的密码不一致", 0, function () {
                    $("#txtConfirmPassword").focus();
                });
                return;
            }

            if (!CheckInput("txtSMSVCode", "请输入手机验证码")) return;
            var usermobileIpt = $("#txtMobile");
            var userpwdIpt = $("#txtPassword");
            var checkcodeIpt = $("#txtSMSVCode");
            $("#nextStep").text("处理中…");

            if (procceing === true) {
                ShowErrorMessage("正在处理中，请稍后...");
                return;
            }

            procceing = true;
            if (!isPass) return;

            var data = { Mobile: usermobileIpt.val(), Password: userpwdIpt.val(), SMSVCode: checkcodeIpt.val() };

            ZYCLib.Web.JsonAjax.NPostData("UserSecureControlService/ResetPassword", data,
                function (jsondata) {
                    if (jsondata != null) {
                        if (jsondata.ErrorCode === 0) {
                            $("#nextStep").text("保存");
                            ShowErrorMessage("您的新密码已经设置成功！页面将在3秒钟后自动跳转至登录页面，点击确定立即前往。", 3, function () {
                                window.location.href = "https://secure" + ZYCLib.Domain + "/signin/login";
                            });
                        } else {
                            $("#nextStep").text("保存");
                            ShowErrorMessage(jsondata.ErrorMessage, null, function () { window.location.href = "http://www" + ZYCLib.Domain + "/WebPage/ForgetPwd"; });
                        }
                    }
                    procceing = false;
                });
        };

        var ForgetPsdStep2Init = function () {
            var bindLoginEventItems = ['txtPassword', 'txtConfirmPassword'];
            $.each(bindLoginEventItems, function (index, val) {
                $("#" + val).on("keypress", function (event) {
                    var ev = event || window.event;
                    if (ev.keyCode === 13) {
                        ResetPassword();
                        return false;
                    }
                    return true;
                });
            });

            $("#nextStep").on("click", function () {
                ResetPassword();
            });

            $("#txtPassword").focus();
        };

        ForgetPsdStep2Init();
    })();
})("Page");